package com.xo.so.vanthan;

import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.message.MessageDialog;
import com.xo.so.R;
import com.xo.so.Variables;
import com.xo.so.jsouplocal.JsoupLocal;
import com.xo.so.model.KetQuaXoSoMienBac;

public class FragmentMienBac extends Fragment {

	Context context;
	private TextView txtMien;
	private TextView txtSo1;
	private TextView txtGiaiNhatSo1;
	private TextView txtGiaiNhiSo1;
	private TextView txtGiaiNhiSo2;
	private TextView txtGiaiBaSo1;
	private TextView txtGiaiBaSo2;
	private TextView txtGiaiBaSo3;
	private TextView txtGiaiBaSo4;
	private TextView txtGiaiBaSo5;
	private TextView txtGiaiBaSo6;
	private TextView txtGiaiBonSo1;
	private TextView txtGiaiBonSo2;
	private TextView txtGiaiBonSo3;
	private TextView txtGiaiBonSo4;
	private TextView txtGiaiNamSo1;
	private TextView txtGiaiNamSo2;
	private TextView txtGiaiNamSo3;
	private TextView txtGiaiNamSo4;
	private TextView txtGiaiNamSo5;
	private TextView txtGiaiNamSo6;
	private TextView txtGiaiSauSo1;
	private TextView txtGiaiSauSo2;
	private TextView txtGiaiSauSo3;
	private TextView txtGiaiBaySo1;
	private TextView txtGiaiBaySo2;
	private TextView txtGiaiBaySo3;
	private TextView txtGiaiBaySo4;

	private TextView txtDuoiCua0;
	private TextView txtDuoiCua1;
	private TextView txtDuoiCua2;
	private TextView txtDuoiCua3;
	private TextView txtDuoiCua4;
	private TextView txtDuoiCua5;
	private TextView txtDuoiCua6;
	private TextView txtDuoiCua7;
	private TextView txtDuoiCua8;
	private TextView txtDuoiCua9;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = null;
		rootView = inflater.inflate(R.layout.fragment_mien_bac, container,
				false);
		context = rootView.getContext();
		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {

		txtMien = (TextView) view.findViewById(R.id.text_title_truyen_thong);
		txtSo1 = (TextView) view.findViewById(R.id.number0);
		txtGiaiNhatSo1 = (TextView) view.findViewById(R.id.number1);
		txtGiaiNhiSo1 = (TextView) view.findViewById(R.id.number2_1);
		txtGiaiNhiSo2 = (TextView) view.findViewById(R.id.number2_2);
		txtGiaiBaSo1 = (TextView) view.findViewById(R.id.number3_1);
		txtGiaiBaSo2 = (TextView) view.findViewById(R.id.number3_2);
		txtGiaiBaSo3 = (TextView) view.findViewById(R.id.number3_3);
		txtGiaiBaSo4 = (TextView) view.findViewById(R.id.number3_4);
		txtGiaiBaSo5 = (TextView) view.findViewById(R.id.number3_5);
		txtGiaiBaSo6 = (TextView) view.findViewById(R.id.number3_6);
		txtGiaiBonSo1 = (TextView) view.findViewById(R.id.number4_1);
		txtGiaiBonSo2 = (TextView) view.findViewById(R.id.number4_2);
		txtGiaiBonSo3 = (TextView) view.findViewById(R.id.number4_3);
		txtGiaiBonSo4 = (TextView) view.findViewById(R.id.number4_4);
		txtGiaiNamSo1 = (TextView) view.findViewById(R.id.number5_1);
		txtGiaiNamSo2 = (TextView) view.findViewById(R.id.number5_2);
		txtGiaiNamSo3 = (TextView) view.findViewById(R.id.number5_3);
		txtGiaiNamSo4 = (TextView) view.findViewById(R.id.number5_4);
		txtGiaiNamSo5 = (TextView) view.findViewById(R.id.number5_5);
		txtGiaiNamSo6 = (TextView) view.findViewById(R.id.number5_6);
		txtGiaiSauSo1 = (TextView) view.findViewById(R.id.number6_1);
		txtGiaiSauSo2 = (TextView) view.findViewById(R.id.number6_2);
		txtGiaiSauSo3 = (TextView) view.findViewById(R.id.number6_3);
		txtGiaiBaySo1 = (TextView) view.findViewById(R.id.number7_1);
		txtGiaiBaySo2 = (TextView) view.findViewById(R.id.number7_2);
		txtGiaiBaySo3 = (TextView) view.findViewById(R.id.number7_3);
		txtGiaiBaySo4 = (TextView) view.findViewById(R.id.number7_4);

		txtDuoiCua0 = (TextView) view.findViewById(R.id.number_loto_number_0);
		txtDuoiCua1 = (TextView) view.findViewById(R.id.number_loto_number_1);
		txtDuoiCua2 = (TextView) view.findViewById(R.id.number_loto_number_2);
		txtDuoiCua3 = (TextView) view.findViewById(R.id.number_loto_number_3);
		txtDuoiCua4 = (TextView) view.findViewById(R.id.number_loto_number_4);
		txtDuoiCua5 = (TextView) view.findViewById(R.id.number_loto_number_5);
		txtDuoiCua6 = (TextView) view.findViewById(R.id.number_loto_number_6);
		txtDuoiCua7 = (TextView) view.findViewById(R.id.number_loto_number_7);
		txtDuoiCua8 = (TextView) view.findViewById(R.id.number_loto_number_8);
		txtDuoiCua9 = (TextView) view.findViewById(R.id.number_loto_number_9);

		KQXSMBLoader kQXSMBLoader = new KQXSMBLoader();
		kQXSMBLoader.execute();

		super.onViewCreated(view, savedInstanceState);
	}

	class KQXSMBLoader extends AsyncTask<Void, Void, KetQuaXoSoMienBac> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

		}

		@Override
		protected KetQuaXoSoMienBac doInBackground(Void... params) {
			return JsoupLocal.parseKQXSMB();
		}

		@Override
		protected void onPostExecute(KetQuaXoSoMienBac kq) {
			if (Variables.Error == true) {
				MessageDialog messageDialog = new MessageDialog(context);
				messageDialog.DialogExit();

			} else {

				if (kq != null) {
					txtMien.setText(kq.getTitle());

					txtSo1.setText(kq.getMaSoTrungThuong(0)[0]);
					txtGiaiNhatSo1.setText(kq.getMaSoTrungThuong(1)[0]); // hú
					txtGiaiNhiSo1.setText(kq.getMaSoTrungThuong(2)[0]);
					txtGiaiNhiSo2.setText(kq.getMaSoTrungThuong(2)[1]);
					txtGiaiBaSo1.setText(kq.getMaSoTrungThuong(3)[0]);
					txtGiaiBaSo2.setText(kq.getMaSoTrungThuong(3)[1]);
					txtGiaiBaSo3.setText(kq.getMaSoTrungThuong(3)[2]);
					txtGiaiBaSo4.setText(kq.getMaSoTrungThuong(3)[3]);
					txtGiaiBaSo5.setText(kq.getMaSoTrungThuong(3)[4]);
					txtGiaiBaSo6.setText(kq.getMaSoTrungThuong(3)[5]);
					txtGiaiBonSo1.setText(kq.getMaSoTrungThuong(4)[0]);
					txtGiaiBonSo2.setText(kq.getMaSoTrungThuong(4)[1]);
					txtGiaiBonSo3.setText(kq.getMaSoTrungThuong(4)[2]);
					txtGiaiBonSo4.setText(kq.getMaSoTrungThuong(4)[3]);
					txtGiaiNamSo1.setText(kq.getMaSoTrungThuong(5)[0]);
					txtGiaiNamSo2.setText(kq.getMaSoTrungThuong(5)[1]);
					txtGiaiNamSo3.setText(kq.getMaSoTrungThuong(5)[2]);
					txtGiaiNamSo4.setText(kq.getMaSoTrungThuong(5)[3]);
					txtGiaiNamSo5.setText(kq.getMaSoTrungThuong(5)[4]);
					txtGiaiNamSo6.setText(kq.getMaSoTrungThuong(5)[5]);
					txtGiaiSauSo1.setText(kq.getMaSoTrungThuong(6)[0]);
					txtGiaiSauSo2.setText(kq.getMaSoTrungThuong(6)[1]);
					txtGiaiSauSo3.setText(kq.getMaSoTrungThuong(6)[2]);
					txtGiaiBaySo1.setText(kq.getMaSoTrungThuong(7)[0]);
					txtGiaiBaySo2.setText(kq.getMaSoTrungThuong(7)[1]);
					txtGiaiBaySo3.setText(kq.getMaSoTrungThuong(7)[2]);
					txtGiaiBaySo4.setText(kq.getMaSoTrungThuong(7)[3]);

					txtDuoiCua0.setText(kq.getCacSoDauDuoiMaGiai(0));
					txtDuoiCua1.setText(kq.getCacSoDauDuoiMaGiai(1));
					txtDuoiCua2.setText(kq.getCacSoDauDuoiMaGiai(2));
					txtDuoiCua3.setText(kq.getCacSoDauDuoiMaGiai(3));
					txtDuoiCua4.setText(kq.getCacSoDauDuoiMaGiai(4));
					txtDuoiCua5.setText(kq.getCacSoDauDuoiMaGiai(5));
					txtDuoiCua6.setText(kq.getCacSoDauDuoiMaGiai(6));
					txtDuoiCua7.setText(kq.getCacSoDauDuoiMaGiai(7));
					txtDuoiCua8.setText(kq.getCacSoDauDuoiMaGiai(8));
					txtDuoiCua9.setText(kq.getCacSoDauDuoiMaGiai(9));

				}
			}
		}

	}

}
